<!--菜单列表-->
<template>
  <div class="menuIndex">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button size="small" @click.native="addMenu">添加菜单</el-button>
      </div>
      <el-table
        :data="menuInfo"
        border
        style="width: 100%">
        <el-table-column
          align=center
          type="index"
          label="编号"
          width="80">
        </el-table-column>
        <el-table-column
          align=center
          prop=name
          label="菜单名称">
        </el-table-column>
        <el-table-column
          align=center
          prop=level
          label="菜单级数">
        </el-table-column>
        <el-table-column
          align=center
          prop=icon
          label="前端图标">
        </el-table-column>
        <el-table-column
          align=center
          prop=orderNum
          label="排序">
        </el-table-column>
        <el-table-column
          align=center
          fixed="right"
          label="操作">
          <template slot-scope="scope">
            <el-button @click="handlemenuEdit(scope.row.id)" type="text" size="small">编辑</el-button>
            <el-button type="text" size="small" @click="menuDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  </el-card>
  </div>
</template>

<script>
// 引入所有菜单接口
import { getMenuAll, menuDelet } from '@/services/menu.js'
export default {
  name: 'menuIndex',
  data () {
    return {
      menuInfo: []
    }
  },
  created () {
    this.getMenuAll()
  },
  methods: {
    // 获取所有的菜单信息
    async getMenuAll () {
      const { data } = await getMenuAll()
      this.menuInfo = data.data
      for (let i = 0; i < this.menuInfo.length; i++) {
        this.menuInfo[i].level === 0 ? this.menuInfo[i].level = '一级' : this.menuInfo[i].level = '二级'
      }
    },
    // 添加菜单
    addMenu () {
      this.$router.push({ name: 'menuDetail' })
    },
    // 删除数据
    menuDel (id) {
      this.$confirm('确定删除吗？', '提示')
        .then(async () => {
          const { data } = await menuDelet(id)
          if (data.code === '000000') {
            const index = this.menuInfo.findIndex(item => item.id === id)
            this.menuInfo.splice(index, 1)
          }
          this.$message.success('删除成功!')
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    // 编辑菜单
    handlemenuEdit (id) {
      this.$router.push({ name: 'menuEditDetail', params: { id } })
    }
    // 添加菜单和编辑菜单是相同的页面布局,
    // 所以通过路由设置跳转不同的页面,但是挂载的组件为同一组件
  }
}

</script>

<style>
</style>
